home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* TstAdlrU *}
- {* Copyright (c) Julian M Bucknall 2000 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Algorithms Alfresco: Adler checksum routines *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- unit TstAdlrU;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- function UpdateAdlerSimple(aAdler : longword;
- var aBuffer; aCount : integer) : longword;
- var
- S1 : longword;
- S2 : longword;
- i : integer;
- Buffer : PChar;
- begin
- S1 := aAdler and $FFFF;
- S2 := aAdler shr 16;
- Buffer := @aBuffer;
- for i := 0 to pred(aCount) do begin
- S1 := (S1 + ord(Buffer^)) mod 65521;
- S2 := (S2 + S1) mod 65521;
- inc(Buffer);
- end;
- Result := (S2 shl 16) or S1;
- end;
-
- function UpdateAdler(aAdler : longword;
- var aBuffer; aCount : integer) : longword;
- var
- S1 : longword;
- S2 : longword;
- i : integer;
- Buffer : PChar;
- begin
- Assert(aCount <= 4096,
- 'the UpdateAdler routine has been optimized for buffers up to 4KB');
- S1 := aAdler and $FFFF;
- S2 := aAdler shr 16;
- Buffer := @aBuffer;
- for i := 0 to pred(aCount) do begin
- inc(S1, ord(Buffer^));
- inc(S2, S1);
- inc(Buffer);
- end;
- S1 := S1 mod 65521;
- S2 := S2 mod 65521;
- Result := (S2 shl 16) or S1;
- end;
-
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- i : integer;
- FS : TFileStream;
- StartTime : longword;
- BytesRead : integer;
- Buffer : array [0..4095] of byte;
- Time1 : longword;
- Time2 : longword;
- Adler1 : longword;
- Adler2 : longword;
- begin
- Time1 := 0;
- Time2 := 0;
- Adler1 := 1;
- Adler2 := 1;
- FS := TFileStream.Create('c:\unsorted.dat', fmOpenRead);
- try
- BytesRead := FS.Read(Buffer, 4096);
- while (BytesRead <> 0) do begin
- StartTime := GetTickCount;
- Adler1 := UpdateAdlerSimple(Adler1, Buffer, BytesRead);
- inc(Time1, GetTickCount - StartTime);
- StartTime := GetTickCount;
- Adler2 := UpdateAdler(Adler2, Buffer, BytesRead);
- inc(Time2, GetTickCount - StartTime);
- BytesRead := FS.Read(Buffer, 4096);
- end;
- finally
- FS.Free;
- end;
- Label3.Caption := IntToStr(Time1);
- Label4.Caption := IntToStr(Time2);
- Assert(Adler1 = Adler2,
- 'the adler checksums do not match');
- end;
-
- end.
-